package com.ruoyi.web.controller.api;


import com.ruoyi.cat.domain.CatApplycashoutTable;
import com.ruoyi.cat.domain.CatUserbasicTable;
import com.ruoyi.cat.domain.VO.WithdrawVO;
import com.ruoyi.cat.service.ICatApplycashoutTableService;
import com.ruoyi.cat.service.ICatChanceService;
import com.ruoyi.cat.service.ICatUserbasicTableService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.WxLoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.OrderUtil;
import com.ruoyi.framework.web.service.WxTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Api(tags = "api-提现相关接口")
@RestController
@RequestMapping("/api/withdrawal")
public class WithdrawalController extends BaseController {

    @Autowired
    private ICatApplycashoutTableService catApplycashoutTableService;


    @Autowired
    private WxTokenService wxTokenService;

    @Autowired
    private ICatUserbasicTableService iCatUserbasicTableService;

    @Autowired
    private ICatChanceService iCatChanceService;

    //提现申请

    @ApiOperation("我的提现记录")
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request, CatApplycashoutTable catApplycashoutTable)
    {
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        if (wxLoginUser != null) {
            catApplycashoutTable.setUserId(wxLoginUser.getUserId());
        }
        startPage();
        List<CatApplycashoutTable> list = catApplycashoutTableService.selectCatApplycashoutTableList(catApplycashoutTable);
        return getDataTable(list);
    }


    @ApiOperation("提现接口")
    @PostMapping("/withdraw")
    public AjaxResult withdraw(HttpServletRequest request, @Validated @RequestBody WithdrawVO withdrawVO){
        CatApplycashoutTable shoppingWithdrawal = new CatApplycashoutTable();
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        shoppingWithdrawal.setUserId(wxLoginUser.getUserId());
        shoppingWithdrawal.setApplyMoney(withdrawVO.getWithdrawalAmount());
        shoppingWithdrawal.setMethod(withdrawVO.getMethod());
        shoppingWithdrawal.setFullName(withdrawVO.getFullName());
        shoppingWithdrawal.setAccountNumber(withdrawVO.getAccountNumber());
        //shoppingWithdrawal.setOrderNo(OrderUtil.makeOrderNo());
        CatUserbasicTable shoppingUser = iCatUserbasicTableService.selectCatUserbasicTableById(wxLoginUser.getUserId());
        if (shoppingUser != null) {
            BigDecimal result =   shoppingUser.getAmount().subtract(withdrawVO.getWithdrawalAmount());
            if (result.compareTo(BigDecimal.ZERO) < 0){
                return error("余额不足");
            }
            shoppingUser.setWithdrawal(result);
        }
        iCatUserbasicTableService.updateCatUserbasicTable(shoppingUser);
        return success(catApplycashoutTableService.insertCatApplycashoutTable(shoppingWithdrawal));
    }

    @ApiOperation("查询提现申请金额")
    @GetMapping("/getWithdrawal")
    public AjaxResult getWithdrawal(HttpServletRequest request)
    {
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        CatApplycashoutTable catApplycashoutTable =  catApplycashoutTableService.getApplyMoney(wxLoginUser.getUserId());
        if (catApplycashoutTable == null) {
            catApplycashoutTable = new CatApplycashoutTable();
            catApplycashoutTable.setApplyMoney(new BigDecimal("0"));
        }
        return success(catApplycashoutTable);
    }


    @ApiOperation("参与用户")
    @GetMapping("/participatingUsers")
    public AjaxResult participatingUsers(HttpServletRequest request,Long articleId)
    {
        return success(iCatChanceService.getChanceData(articleId));
    }





}
